package com.gwsdk.paysdk.utils;

import android.content.Context;
import android.os.CountDownTimer;

import com.gwsdk.open.AnnounceTimeCallBack;
import com.gwsdk.open.GWApi;
import com.gwsdk.paysdk.bean.AntiAddictionModel;
import com.gwsdk.paysdk.entity.AntiAddiction;
import com.gwsdk.paysdk.floatview.GWPreventionHaredView;

/**
 * Created by Administrator on 2017/5/4.
 * 防沉迷计时
 */

public class MyTimeUtil {

    public static final String TAG = "MyTimeUtil";
    private final Context context;
    private AntiAddiction antiAddiction;
    //第一次提示时间
    private long firstTime;
    //第二次提示时间
    private long secondTime;

    //游戏时间（分钟）
    private long playTime;
    private long downTime;
    private TimeFristCount timeFristCount;
    private TimeSecondCount timeSecondCount;
    private TimeDown timeDown;

    public MyTimeUtil(final Context context, AntiAddiction anti_Addiction) {
        this.context = context;
        this.antiAddiction = anti_Addiction;

        firstTime = (long) AppUtils.getDouble(antiAddiction.getHours_off_one());
        secondTime = (long) AppUtils.getDouble(antiAddiction.getHours_off_two());
        playTime = (long) antiAddiction.getPlay_time();
        downTime = (long) AppUtils.getDouble(antiAddiction.getBat());

        GWLog.i(TAG,"已经在线时间：" + playTime + "，首次提示时间：" + firstTime + "，第二次提示时间：" + secondTime + "，强制下线时间：" + downTime);
    }

    public void start(AntiAddiction antiAddiction) {
        GWLog.w(TAG, "开始计时");
        if (antiAddiction == null) {
            GWLog.e(TAG, "antiAddictions is null");
            return;
        }
        //todo 如果  在线时间 小于 第一次提示时间
        if(playTime<firstTime){

            //计算距离第一次提示剩余时间
            long l = firstTime - playTime;
            timeFristCount = new TimeFristCount(l * 1000, 1000);
            timeFristCount.start();
        }

        //todo 如果  在线时间 小于 第二次提示时间
        if(playTime<secondTime){

            //计算距离第二次提示剩余时间
            long l = secondTime - playTime;
            timeSecondCount = new TimeSecondCount(l * 1000, 1000);
            timeSecondCount.start();
        }

        //todo 如果  在线时间 小于 强制下线时间
        if (playTime<downTime){
            //计算距离强制下线剩余时间
            long l = downTime - playTime;
            timeDown = new TimeDown(l * 1000, 1000);
            timeDown.start();
        }

    }

    public void Stop() {
        GWLog.i(TAG,"停止计时");
        if(timeFristCount!=null){
            timeFristCount.cancel();
            timeFristCount = null;
        }
        if(timeSecondCount!=null){
            timeSecondCount.cancel();
            timeSecondCount = null;
        }
        if(timeDown!=null){
            timeDown.cancel();
            timeDown = null;
        }


//        if (getPlca() != null) {
//            //此时游戏为在后台运行或退出状态
//            getPlca().callback("3");
//        }
    }

    class TimeFristCount extends CountDownTimer {
        public TimeFristCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            //todo 到了第一次提示时间
            GWLog.e(TAG,"到了第一次提醒时间");
            if (getPlca() != null) {
                getPlca().callback("1");
            }
            if (!TextUtils.isEmpty(antiAddiction.getContents_one())) {
//                DialogUtil.showCustomMsg(context, "提示", antiAddiction.getContents_one(), context, "我知道了", "", false);
                GWPreventionHaredView.getInstance(context,5000,antiAddiction.getContents_one()).show();
            }
        }
    }


    class TimeSecondCount extends CountDownTimer {
        public TimeSecondCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            //todo 到了第二次提示时间
            GWLog.e(TAG,"到了第二次提醒时间");
            if (getPlca() != null) {
                getPlca().callback("2");
            }
            if (!TextUtils.isEmpty(antiAddiction.getContents_two())) {
//                DialogUtil.showCustomMsg(context, "提示", antiAddiction.getContents_two(), context, "我知道了", "", false);
                GWPreventionHaredView.getInstance(context,5000,antiAddiction.getContents_two()).show();
            }
        }
    }


    class TimeDown extends CountDownTimer {
        public TimeDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            //todo 到了强制下线时间
            GWLog.e(TAG,"到了强制下线时间");
            if (getPlca() != null) {
                getPlca().callback("3");
            }

            AntiAddictionModel.getInstance().showDownDialog(GWApi.getApi().getLogoutClaaback(),"由于您未进行实名认证，当前游戏时长已到达防沉迷限制，请下线休息");
        }
    }


    public AnnounceTimeCallBack getPlca() {
        if (GWApi.getApi().getAnnounceTimeCallBack() != null) {
            return GWApi.getApi().getAnnounceTimeCallBack();
        } else {
            return null;
        }

    }

}
